<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图书管理</title>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.7.0/themes/metro-blue/easyui.css">
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.7.0/themes/icon.css">
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/jquery.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript">
        var url;

        function searchBook() {
            $("#dg").datagrid('load', {
                "name": $("#s_name").val(),
                "bookTypeId": $("#s_bookTypeId").val(),
                "state": $("#s_state").val(),
                "bookNumber": $("#s_bookNumber").val()
            });
        }

        function resetValue() {

            $("#s_name").val("");
            $("#s_bookNumber").val("");
            $("#s_bookTypeId").combobox("setValue", "");
            $("#s_state").combobox("setValue", "");
            $("#bookTypeId").combobox("setValue", "");
            $("#name").val("");
            $("#bookNumber").val("");
            $("#location").val("");
            $("#bookDescribe").val("");
            $("#bookImage").val("");
        }

        function closeBookDialog() {
            $("#dlg").dialog("close");
            resetValue();
        }

        function saveBook() {
            $("#fm").form("submit", {
                url: url,
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.success) {
                        $.messager.alert("系统提示", "保存成功！");
                        resetValue();
                        $("#dlg").dialog("close");
                        $("#dg").datagrid("reload");
                    } else {
                        $.messager.alert("系统提示", result.errorInfo);
                    }
                }
            });
        }

        function openBookAddDialog() {
            $("#dlg").dialog("open").dialog("setTitle", "添加图书信息");
            url = "/admin/book/save";
        }

        function openBookModifyDialog() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一条要修改的数据！");
                return;
            }
            var row = selectedRows[0];
            $("#dlg").dialog("open").dialog("setTitle", "修改图书信息");
            $("#fm").form("load", row);
            url = "/admin/book/save?id=" + row.id;
        }

        function deleteBook() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择要删除的数据！");
                return;
            }
            var strIds = [];
            for (var i = 0; i < selectedRows.length; i++) {
                strIds.push(selectedRows[i].id);
            }
            var ids = strIds.join(",");
            $.messager.confirm("系统提示", "您确定要删除这<span style='color: red'>" + selectedRows.length + "</span>条数据吗?", function (r) {
                if (r) {
                    $.post("/admin/book/delete", {ids: ids}, function (result) {
                        if (result.success) {
                            $.messager.alert("系统提示", "数据已成功删除！");
                            $("#dg").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    }, "json");
                }
            });
        }

        $(document).ready(function () {

            $("#dg").datagrid({
                onDblClickRow: function (index, row) {
                    $("#dlg").dialog("open").dialog("setTitle", "修改图书信息");
                    $("#fm").form("load", row);
                    url = "/admin/book/save?id=" + row.id;
                }
            });

        });

        function formatState(val, row) {
            if (val == 1) {
                return "<span style='color: #0c9076'>可借阅</span>";
            } else if (val == 2) {
                return "<span style='color: blue'>已借出</span>";
            } else {
                return "<span style='color: red'>不可借阅</span>";
            }
        }

        function formatName(val, row) {
            if (val.length > 20) {
                return val.substring(0, 20) + "...";
            } else {
                return val;
            }
        }

        function formatImage(val, row) {
            return "<img height=122 src='/static/images/bookImage/"+val+"'/>";
        }


        function setBookCanBorrowOrNot() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一本图书！");
                return;
            }
            var id = selectedRows[0].id;
            var state = selectedRows[0].state;
            if (state == 2) {
                $.messager.alert("系统提示", "无法设置已经被借阅的图书！");
                return;
            }
            if (state == 1) {
                $.messager.confirm("系统提示", "您确定要将这本图书的状态设置为<span style='color: red'>不可借阅</span>吗?", function (r) {
                    if (r) {
                        $.post("/admin/book/setBookCanBorrowOrNot", {bookId: id, state: 3}, function (result) {
                            if (result.success) {
                                $.messager.alert("系统提示", "设置成功！");
                                $("#dg").datagrid("reload");
                            } else {
                                $.messager.alert("系统提示", "设置失败！");
                            }
                        }, "json");
                    }
                });
            } else {
                $.messager.confirm("系统提示", "您确定要将这本图书的状态设置为<span style='color: red'>可借阅</span>吗?", function (r) {
                    if (r) {
                        $.post("/admin/book/setBookCanBorrowOrNot", {bookId: id, state: 1}, function (result) {
                            if (result.success) {
                                $.messager.alert("系统提示", "设置成功！");
                                $("#dg").datagrid("reload");
                            } else {
                                $.messager.alert("系统提示", "设置失败！");
                            }
                        }, "json");
                    }
                });
            }
        }
    </script>
</head>
<body style="margin: 1px">
<table id="dg" title="图书管理" class="easyui-datagrid" striped="true"
       fitColumns="true" pagination="true" rownumbers="true"
       url="/admin/book/list" fit="true" toolbar="#tb">
    <thead>
    <th field="cb" checkbox="true" align="center"></th>
    <th field="id" width="20" align="center" hidden="hidden">编号</th>
    <th field="imageName" width="40" align="center" formatter="formatImage">图片</th>
    <th field="name" width="70" align="center" formatter="formatName">名称</th>
    <th field="bookTypeName" width="30" align="center">图书类别名称</th>
    <th field="bookNumber" width="30" align="center">书号</th>
    <th field="state" width="20" align="center" formatter="formatState">状态</th>
    <th field="location" width="30" align="center">图书位置</th>
    </thead>
</table>

<div id="tb">
    <div>
        <a href="javascript:openBookAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
        <a href="javascript:openBookModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit"
           plain="true">修改</a>
        <a href="javascript:deleteBook()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
        <a href="javascript:setBookCanBorrowOrNot()" class="easyui-linkbutton" iconCls="icon-prohibitBorrow" plain="true">不可借阅与否</a>
    </div>
    <div>
        &nbsp;图书名称：&nbsp;<input type="text" id="s_name" size="20" onkeydown="if(event.keyCode==13) searchBook()"/>
        &nbsp;书号：&nbsp;<input type="text" id="s_bookNumber" size="20" onkeydown="if(event.keyCode==13) searchBook()"/>
        &nbsp;图书类型:&nbsp;<input class="easyui-combobox" id="s_bookTypeId" style="width: 140px" editable="false"
                                data-options="valueField:'id',textField:'name',url:'/admin/bookType/comboList'"/>
        &nbsp;图书状态:&nbsp;
        <select id="s_state" class="easyui-combobox" editable="false" style="width:100px;" data-options="panelHeight:'auto'">
            <option value="">请选择...</option>
            <option value="1">可借阅</option>
            <option value="2">已借出</option>
        </select>
        <a href="javascript:searchBook()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
        <a href="javascript:resetValue()" class="easyui-linkbutton" iconCls="icon-reset" plain="true">重置</a>
    </div>
</div>


<div id="dlg" class="easyui-dialog" style="width: 620px;height: 400px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons" data-options="onClose:function(){resetValue()}">
    <form id="fm" method="post" enctype="multipart/form-data">
        <table cellspacing="8px">
            <tr>
                <td valign="top">名称:</td>
                <td>
                    <input id="name" name="name" class="easyui-validatebox" required="true" style="width: 400px">
                </td>
            </tr>
            <tr>
                <td>图片：</td>
                <td>
                    <input type="file" id="bookImage" name="bookImage"/>
                </td>
            </tr>
            <tr>
                <td valign="top">图书类别:</td>
                <td>
                    <input class="easyui-combobox" id="bookTypeId" name="bookTypeId" required="true"
                           style="width: 400px" editable="false"
                           data-options="valueField:'id',textField:'name',url:'/admin/bookType/comboList'"/>
                </td>
            </tr>
            <tr>
                <td valign="top">书号:</td>
                <td>
                    <input id="bookNumber" name="bookNumber" class="easyui-validatebox" required="true"
                           style="width: 400px">
                </td>
            </tr>
            <tr>
                <td valign="top">图书位置:</td>
                <td>
                    <input id="location" name="location" class="easyui-validatebox" required="true"
                           style="width: 400px">
                </td>
            </tr>
            <tr>
                <td valign="top">描述:</td>
                <td>
                    <textarea rows="8" cols="65" id="bookDescribe" name="bookDescribe" class="easyui-validatebox"
                              required="true"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

<div id="dlg-buttons">
    <a href="javascript:saveBook()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:closeBookDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
</body>
</html>